#include "servermonitor.h"

#include <QNetworkReply>
#include <QMessageBox>

ServerMonitor::ServerMonitor(Ui::Index *ui,QObject *parent)
    : QObject{parent}
{
    this->ui = ui;

}

void ServerMonitor::init()
{
    QNetworkReply *r = serverMonitorApi.server();
    QByteArray bytes = r->readAll().data();
    if(bytes.isEmpty()){
        QMessageBox::warning(0,"提示","服务器连接失败");
        return;
    }
    QJsonObject res = QJsonDocument::fromJson(bytes).object();
    if(200 != res.value("code").toInt()){
        QMessageBox::warning(0,"提示",res.value("msg").toString());
        return;
    }
    QJsonObject obj = res.value("data").toObject();
    initCpu(obj);
    initMem(obj);
    initServer(obj);
    initJava(obj);
    initDrive(obj);
}

void ServerMonitor::initCpu(QJsonObject obj)
{
    QJsonObject cpu = obj.value("cpu").toObject();
    ui->label_serverMonitor_coreCount->setText(QString::number(cpu.value("cpuNum").toInt()));
    ui->label_serverMonitor_userUsageRate->setText(QString::number(cpu.value("used").toDouble()) + "%");
    ui->label_serverMonitor_sysUsageRate->setText(QString::number(cpu.value("sys").toDouble()) + "%");
    ui->label_serverMonitor_freeRate->setText(QString::number(cpu.value("free").toDouble()) + "%");
}

void ServerMonitor::initMem(QJsonObject obj)
{
    QJsonObject mem = obj.value("mem").toObject();
    double residue = mem.value("total").toDouble() - mem.value("used").toDouble();
    ui->label_serverMonitor_mem_memCount->setText(QString::number(mem.value("total").toDouble()) + "G");
    ui->label_serverMonitor_mem_usageMem->setText(QString::number(mem.value("used").toDouble()) + "G");
    ui->label_serverMonitor_mem_residueMem->setText(QString::number(residue) + "G");
    ui->label_serverMonitor_memRate->setText(QString::number(mem.value("usage").toDouble()) + "%");
    QJsonObject jvm = obj.value("jvm").toObject();
    residue = jvm.value("total").toDouble() - jvm.value("used").toDouble();
    ui->label_serverMonitor_JVM_memCount->setText(QString::number(jvm.value("total").toDouble()) + "M");
    ui->label_serverMonitor_JVM_usageMem->setText(QString::number(jvm.value("used").toDouble()) + "M");
    ui->label_serverMonitor_JVM_residueMem->setText(QString::number(residue) + "M");
    ui->label_serverMonitor_JVMRate->setText(QString::number(jvm.value("usage").toDouble()) + "%");
}

void ServerMonitor::initServer(QJsonObject obj)
{
    QJsonObject server = obj.value("sys").toObject();
    ui->label_serverMonitor_serverName->setText(server.value("computerName").toString());
    ui->label_serverMonitor_serverIP->setText(server.value("computerIp").toString());
    ui->label_serverMonitor_OS->setText(server.value("osName").toString());
    ui->label_serverMonitor_sysArchitecture->setText(server.value("osArch").toString());
}

void ServerMonitor::initJava(QJsonObject obj)
{
    QJsonObject server = obj.value("sys").toObject();
    ui->label_serverMonitor_projectPath->setText(server.value("userDir").toString());
    QJsonObject jvm = obj.value("jvm").toObject();
    ui->label_serverMonitor_JavaName->setText(jvm.value("name").toString());
    ui->label_serverMonitor_startTime->setText(jvm.value("startTime").toString());
    ui->label_serverMonitor_installPath->setText(jvm.value("home").toString());
    ui->label_serverMonitor_runParams->setText(jvm.value("inputArgs").toString());
    ui->label_serverMonitor_JavaVersion->setText(jvm.value("version").toString());
    ui->label_serverMonitor_runTime->setText(jvm.value("runTime").toString());
}

void ServerMonitor::initDrive(QJsonObject obj)
{
    QJsonArray driveArr = obj.value("sysFiles").toArray();
    QJsonObject drive = driveArr.at(0).toObject();
    ui->label_serverMonitor_driveLetterPath->setText(drive.value("dirName").toString());
    ui->label_serverMonitor_fileSystem->setText(drive.value("sysTypeName").toString());
    ui->label_serverMonitor_driveLetterType->setText(drive.value("typeName").toString());
    ui->label_serverMonitor_countDrive->setText(drive.value("total").toString());
    ui->label_serverMonitor_residueDrive->setText(drive.value("free").toString());
    ui->label_serverMonitor_useDrive->setText(drive.value("used").toString());
    ui->label_serverMonitor_driveUsageRate->setText(QString::number(drive.value("usage").toDouble()) + "%");
}
